package com.gsj.system.service.impl;

import com.alibaba.cloud.commons.lang.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gsj.common.pojo.R;
import com.gsj.system.eneity.Role;
import com.gsj.system.mapper.RoleMapper;
import com.gsj.system.pojo.RoleVo;
import com.gsj.system.pojo.RolevoSecond;
import com.gsj.system.service.RoleService;
import com.gsj.system.utils.SystemTimeUtil;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @program: clmseighth
 * @description:
 * @author: 张巍蓝
 * @create: 2021-07-24 09:25
 **/
@Service
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleMapper roleMapper;
    @Override
    public R queryBYuserfRole(Integer userId) {
        List<Role> menus = roleMapper.queryBYuserfRole(userId);
        ArrayList<Integer> roles = new ArrayList<>();
        for(Role menu2:menus){
            Integer roleId = menu2.getRoleId();
            roles.add(roleId);
        }
        List<Role> role = roleMapper.queryalluserfRole();
        return R.ok().put("role",roles).put("roleall",role);
    }
    @Override
    public R confirmFenMenu(Integer userId,List<Integer> userRoleIds) {
        //1.删除
        roleMapper.deletebyUserid(userId);
        //2.添加
        roleMapper.insertUserRole(userId,userRoleIds);
        return R.ok();
    }
    @Override
    public R editUserRole(Integer userId, List<Integer> role) {

        roleMapper.deleterolebyuser(userId);
        roleMapper.insertbyUserid(userId,role);
        return null;

    }


    @Override
    public R  queryAllRole(Integer currentPage, Integer pageSize, RoleVo roleVo) {
        Page<Role> userPage = new Page<>(currentPage, pageSize);
        QueryWrapper<Role> userQueryWrapper = new QueryWrapper<>();
        if(StringUtils.isNotEmpty(roleVo.getRoleName())){
            userQueryWrapper.like("role_name",roleVo.getRoleName());
        }
        if(StringUtils.isNotEmpty(roleVo.getCreateBy())) {
            userQueryWrapper.like("create_by", roleVo.getCreateBy());
        }
        Page<Role> userPage1 = roleMapper.selectPage(userPage, userQueryWrapper);
        return R.ok().put("records",userPage1.getRecords()).put("total",userPage1.getTotal());
    }

    @Override
    public R editRoleStatus(Integer id, Integer status) {
        roleMapper.editRoleStatus(id,status);
        return null;
    }

    @Override
    public R queryRoleByRoleId(Integer id) {
        List<Role> roles = roleMapper.queryRoleByRoleId(id);
        return R.ok().put("rolebyid",roles);
    }

    @Override
    public R editRoleByRoleId(Role role, String username) {
        Integer roleId = role.getRoleId();
        String roleName1 = role.getRoleName();
        List<Role> roles = roleMapper.queryRoleByRoleId(roleId);
        String roleName = roles.get(0).getRoleName();
        List<Role> queryall = roleMapper.queryall();
        if(roleName.equals(roleName1)){
            role.setUpdateTime(SystemTimeUtil.getTime());
            roleMapper.editRoleByRoleId(role,username);
            return R.ok().put("message","修改成功");
        }
        for(Role roless:queryall){
            if(roless.getRoleName().equals(roleName1)){
                return R.ok().put("message","角色名重复");
            }
        }
        role.setUpdateTime(SystemTimeUtil.getTime());
        roleMapper.editRoleByRoleId(role,username);
        return R.ok().put("message","修改成功");

    }

    @Override
    public R addNewRole(RolevoSecond role, String username) {
        List<Role> queryall = roleMapper.queryall();
        for(Role roless:queryall){
            if(roless.getRoleName().equals(role.getRoleName())){
                return R.ok().put("message","角色名重复");
            }
        }
        role.setCreateTime(SystemTimeUtil.getTime());
        roleMapper.addNewRole(role,username);
        return R.ok().put("message","新增成功");
    }

    @Override
    public R deleteRoleByRoleId(Integer roleid) {
        roleMapper.deletebyRoleId(roleid);
        return null;
    }




}
